<?php

namespace openapi\models;

use peng\enums\DBOperator;
use peng\helpers\ArrayHelper;
use peng\helpers\Config;

/**
 * 输入基类
 *
 * @author peng.huang
 */
class BaseInput extends \common\base\BaseModel {

    /**
     * 验证串
     * 
     * @var string 
     */
    public $key;

    /**
     * 令牌
     * 
     * @var string 
     */
    public $token;

    /**
     * 当前时间的 Unix 时间戳，即从 Unix 纪元（格林威治时间 1970 年 1 月 1 日 00:00:00）到当前时间的秒数
     * 
     * @var int 
     */
    public $timestamp;

    /**
     * 构造函数
     */
    public function __construct() {
        $this->timestamp = time();
    }

    /**
     * 是否保存
     * 
     * @param object $model [模型对象]
     * @param string $attr [属性名称]
     * @return boolean
     */
    public function isSaved($model, $attr) {
        return DBOperator::INSERT === $model->operator || DBOperator::UPDATE === $model->operator;
    }

    /**
     * 验证图形验证码
     * 
     * @param string $cookieName [cookie的名称]
     * @param string $encryptKey [加密字符串]
     * @return boolean
     */
    public function checkImgCode($cookieName = NULL, $encryptKey = NULL) {
        if (!isset($cookieName)) {
            $config = Config::get('cookie', 'account');
            $cookieName = ArrayHelper::get($config, 'reg_code_name');
            $encryptKey = ArrayHelper::get($config, 'encryption_key');
        }

        $vcode = \Peng::$app->request->cookie($cookieName);
        if (!isset($vcode[0]) || $vcode !== md5(strtolower($this->imgcode) . $encryptKey)) {
            return $this->appendError('imgcode', '图形验证码错误。');
        }

        return TRUE;
    }

    /**
     * 验证令牌
     * 
     * @return boolean
     */
    public function checkTokenValid() {
        /*
          $tokenBiz = new \common\biz\TokenBiz();
          if (!isset($this->token) || !$tokenBiz->isEffective($this->token)) {
          return $this->appendError('token', '令牌无效。');
          }

         */

        return TRUE;
    }

    /**
     * 验证短信码
     * 
     * @param int $keyType [钥匙类型(0=手机注册、1=手机无密码登录、2=手机找回密码、3=email激活、3=email找回密码)]
     * @return boolean
     */
    public function checkSmsCodeValid($keyType) {
        $identifyBiz = new \common\biz\IdentifycodeBiz();
        $this->smscodeRow = $identifyBiz->findCode($this->loginname, $keyType);
        if (empty($this->smscodeRow) || $this->smscode != $this->smscodeRow ['keycode']) {
            return $this->appendError('smscode', '短信验证码错误。');
        }

        return TRUE;
    }

    /**
     * 处理额外的操作
     */
    public function processExtra() {
        // 短信码置成已使用
        if (isset($this->smscodeRow) && $this->smscodeRow) {
            $identifyBiz = new \common\biz\IdentifycodeBiz();
            $identifyBiz->dao->save(array(
                'id' => $this->smscodeRow['id'],
                'status' => 2, // 已经使用
            ));
        }

        // 保存登录
        if (isset($this->savedlogon) && $this->savedlogon) {
            \Peng::$app->controller->writeAccount(array('aid' => $this->aid,
                'loginname' => $this->loginname,
                'loginAt' => time(),
            ));
        }
    }

}
